import registry as reg
import streamlit as st
from st_bridge import bridge, html
import json
import time
import re

columnWidth = [3, 2, 1, 1]

def drawTables(clickLin: dict, bname: str = "Home", view = True, delete = True):
    """
    结构:
    [
        {
            "name": "test",
            "version": [ "1.0.0" ],
            "time": "2021-01-01",
            "key": "test",
        },
        ...
    ]
    """
    # __subject(__line(1, "test", "1.0.0", "2021-01-01", "test", "bridge"))
    allLine = ""
    for i in range(len(clickLin)):
        oneData = clickLin[i]
        allLine += __line(
            i + 1,
            oneData["name"],
            oneData["version"][-1],
            oneData["time"],
            oneData["key"],
            bname,
            view,
            delete,
        )
    __subject(allLine)
    pass


def getHistoryNewTime(manifest: dict) -> str:
    """
    获取最新历史时间
    """
    history: list = manifest["history"]
    v1Compatibility: dict = json.loads(history[0]["v1Compatibility"])
    if v1Compatibility is None:
        return ""
    # NewCreateTime = 2021-11-24T20:19:40.483367546Z
    NewCreateTime: str = v1Compatibility["created"]
    NewCreateTime = re.match(
        r"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}",
        NewCreateTime,
    ).group(0)
    # 转换时间格式 2021年11月24日 20时19分40秒
    NewCreateTime = time.strftime(
        "%Y年%m月%d日 %H时%M分%S秒", time.strptime(NewCreateTime, "%Y-%m-%dT%H:%M:%S.%f")
    )
    return NewCreateTime


def findDataByName(name: str, data: list[dict]):
    for one in data:
        if one["name"] == name:
            return one
    return None


def __line(number: int, name: str, version: str, time: str, key: str, bname: str, view: bool,delete: bool):
    buttonTemplate = f"""
    <tr>
          <td>{number}</td>
          <td>{name}</td>
          <td>{version}</td>
          <td>{time}</td>
          <td>
    """
    if view:
        buttonTemplate += f"""
          <button class="view" onClick="stBridges.send('{bname}', 'view:{key}')">查看</button>
        """
    if delete:
        buttonTemplate += f"""
          <button class="delete" onClick="stBridges.send('{bname}', 'delete:{key}')">删除</button>
        """
    buttonTemplate += """
             </td>
        </tr>
    """
    return buttonTemplate


def __subject(content: str):
    html(
        """
        <table>
      <thead>
        <tr>
          <th>序号</th>
          <th>名称</th>
          <th>版本</th>
          <th>时间</th>
          <th>操作</th>
        </tr>
      <thead>
      <tbody>
        """
        + content
        + """
      </tbody>
    <table/>
      
    <style>
    html,
    body {
        height: 100%;
    }
    body {
        margin: 0;
        # background: linear-gradient(45deg, #49a09d, #5f2c82);
        font-family: sans-serif;
        font-weight: 100;
    }
    .container {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    table {
        width: 100%;
        border-collapse: collapse;
        overflow: hidden;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
    }
    th,
    td {
        padding: 15px;
        background-color: rgba(255, 255, 255, 0.2);
        color: black;
    }
    th {
        text-align: left;
    }
    thead th {
        background-color: #55608f;
        color: #fff;
    }
    tbody tr:hover {
        background-color: rgba(255, 255, 255, 0.3);
    }
    tbody td {
        position: relative;
    }
    tbody td:hover:before {
        content: "";
        position: absolute;
        left: 0;
        right: 0;
        top: -9999px;
        bottom: -9999px;
        background-color: rgba(255, 255, 255, 0.2);
        z-index: -1;
    }
    :root {
        --primary-light: #8abdff;
        --primary: #6d5dfc;
        --primary-dark: #5b0eeb;
        --white: #FFFFFF;
        --greyLight-1: #E4EBF5;
        --greyLight-2: #c8d0e7;
        --greyLight-3: #bec8e4;
        --greyDark: #9baacf;
    }
    .view {
        font-family: monospace;
        background-color: #f3f7fe;
        color: #3b82f6;
        border: none;
        border-radius: 8px;
        width: 65px;
        height: 36px;
        transition: .3s;
    }

    .view:hover {
        background-color: #3b82f6;
        box-shadow: 0 0 0 5px #3b83f65f;
        color: #fff;
    }
    .delete {
        font-family: monospace;
        background-color: #ff000045;
        color: #3b82f6;
        border: none;
        border-radius: 8px;
        width: 65px;
        height: 36px;
        transition: .3s;
    }

    .delete:hover {
        background-color: #f00;
        box-shadow: 0 0 0 5px #ff000045;
        color: #fff;
    }

    
    </style>
    """
    )
